package com.example.demoes;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.springframework.util.StringUtils;

import java.util.Scanner;

/**
 * @version 1.0.0
 * @className: CodeGenerator
 * @description: Mybatis-Generator 代码自动生成
 * @author: zhangjunfa
 * @date: 2022/6/25 10:27
 */
public class CodeGenerator {

    //数据库连接
    // private static String url = "jdbc:mysql://192.168.1.64:3306/nccqjy-platform?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull";//数据库url
    private static String url = "jdbc:mysql://121.36.97.238:3306/mall?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull";//数据库url
    private static String username = "root";//账号
    // private static String password = "root";//密码
    private static String password = "root@1234#RossZhang";//密码

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scannerTable(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.hasText(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    /**
     * 读取作者信息
     *
     * @return
     */
    public static String scannerAuthor() {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入作者：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.hasText(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的作者名称！");
    }

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");
        //开始生成
        FastAutoGenerator.create(url, username, password)
                //全局配置
                .globalConfig(builder -> {
                    builder.author(scannerAuthor())
                            .outputDir(projectPath + "/demo-es/src/main/java")
                            .enableSwagger()//开启swagger
                            .commentDate("yyyy-MM-dd");//注释日期
                })
                //包配置
                .packageConfig(builder -> {
                    builder.parent("com.example.demoes")
                            // .moduleName(moduleName)
                            .entity("entity")
                            .mapper("mapper")
                            .xml("mapper.xml")
                            .service("service")
                            .serviceImpl("service.impl");
                            // .controller("controller");
                })
                //策略配置
                .strategyConfig(builder -> {
                    builder.addInclude(scannerTable("表名，多个英文逗号分割").split(","))
                            //开启生成实体类
                            .entityBuilder()
                            .enableLombok()//开启 lombok 模型
                            .enableTableFieldAnnotation()//开启生成实体时生成字段注解
                            //开启生成mapper
                            .mapperBuilder()
                            .enableBaseResultMap()//启用 BaseResultMap 生成
                            .superClass(BaseMapper.class)//设置父类
                            // .enableMapperAnnotation()//开启 @Mapper 注解
                            .formatMapperFileName("%sMapper")//格式化 mapper 文件名称
                            .formatXmlFileName("%sMapper")//格式化 xml 实现类文件名称
                            //开启生成service及impl
                            .serviceBuilder()
                            .formatServiceFileName("%sService")//格式化 service 接口文件名称
                            .formatServiceImplFileName("%sServiceImpl")//格式化 service 实现类文件名称
                            //开启生成controller
                            .controllerBuilder()
                            // 映射路径使用连字符格式，而不是驼峰
                            .enableHyphenStyle()
                            // .formatFileName("%sController")//格式化文件名称
                            .enableRestStyle();
                    ;
                })
                .templateEngine(new VelocityTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                //.templateConfig(builder -> builder.controller(""))//关闭生成controller
                .execute();

    }

}
